﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Glucometer.ModuleHandler
{
    class UploadingModuleHandler : GlucometerModuleHandler
    {
        private bool uploadingStarted = false;

        private bool uploadingStillInProgress = false;

        public UploadingModuleHandler(IHarware hardware, DeviceMemory memory, DeviceSettings setting, DeviceStatus status)
            : base(hardware,memory,setting, status) 
        {
            _deviceMemory.TransferCompleted += new d_EmptyArgument(_deviceMemory_TransferCompleted);
        }

        void _deviceMemory_TransferCompleted()
        {
            //WHEN THE TRANSFER IS COMPLETE MARK THE VARIABLES
            uploadingStillInProgress = false;
            //DELETE THE RECORDS FROM MEMORY
            _deviceMemory.ClearMemoryRecord();
            //BLINK U SYMBOL
            _hardware.BlinkMode(OperationMode.UPLOADING);
            //THE DEVICE SHOULD END AUTOMATICALLY AFTER 30 SEC
            restartIdlePeriodCounter(IDLE_TIME_INTERVAL);
        }

        public override bool handleEventInMode(SoftwareEventsType set)
        {
            if (set == SoftwareEventsType.USB_REMOVED)
            {
                //STOP THE TRANSFER IF IN PROGRESS
                if(uploadingStillInProgress)
                    _deviceMemory.StopTransfer();
                //END THE DEVICE VOLUNTARILY
                endDevice(EndingType.VOLUNTARY, OperationMode.UPLOADING);
                return true;
            }
            else if (set == SoftwareEventsType.SHORT_CLICK)
            {
                if (!uploadingStarted)
                {
                    uploadingStarted = true;
                    uploadingStillInProgress = true;
                    _deviceMemory.StartTransfer();
                    //RESTART THE IDLE PERIOD COUNTER TO 0 i.e. INFINITY SO THAT IT DOESNOT AUTO END
                    //restartIdlePeriodCounter(0);
                    return true;
                }
            }
            return false;
        }

        public override bool doStatusCheck(SoftwareEventsType set)
        {
            //CHECK IF THE TEST STRIP IS STILL INSERTED
            if (_deviceStatus.testStripInserted)
            {
                setLastError(ErrorCode.METERSTATUSCHECK_ERROR, "Test strip still inserted.");
                return false;
            }

            //STATUS CHECK COMPLETE AND PASS SO RETURN TRUE
            return true;
        }

        public override bool doModeValidation(SoftwareEventsType set)
        {
            //CHECK IF THE USB IS CONNECTED OR NOT AND            
            if(!_deviceStatus.usbConnected)
            {
                setLastError(ErrorCode.MODE_VALIDATION_ERROR, "USB not connected in uploading mode.");
                return false;
            }

            //CHECK IF THERE IS ANY RECORD OR NOT
            if(_deviceMemory.getRecordMemoryCount() == 0)
            {
                setLastError(ErrorCode.MODE_VALIDATION_ERROR, "No record to display.");
                return false;
            }

            //CHECK IF THE APPLICATION IN PC IS RUNNING

            //IF REACHED HERE THEN MODE VALIDATION PASS OTHERWISE FAIL
            return true;
        }

        public override void loadMode()
        {
            //INITIALIZE THE VARIABLE TO NOTE UPLOADING HAS NOT STARTEd
            uploadingStarted = false;
            uploadingStillInProgress = false;
        }
    }
}